OrderExcel = function(A) {
	this.removeAll();
	if (A && A.processStore) {
		this.tempStore = A.processStore;

		this.readTag();
	}
	if (A && A.okList)
		this.okList = A.okList;
	if (A && A.fileName) {
		this.fileName = A.fileName;
		this.initReadFromExcel();
		// this.initUI();
	}
};
OrderExcel.prototype = {
	outName : [],
	outData : [],
	inData : [],
	defaultConfig : {
		start_row : 4,
		ssign_col : 1,
		factory_col : 2,
		price_col : 8
	},
	getInData : function() {
		return this.inData;
	},
	getInDate : function() {
		return this.date1;
	},
	setFunction : function(A) {
		this.readFunction = A.func;
		this.readRec = A.ret;
	},
	cleanUp : function() {
		window.clearInterval(idTmr);
		CollectGarbage();
	},
	setFileName : function(A) {
		logger.debug("setFileName:" + A);
		this.fileName = A;
		this.initReadFromExcel();
		return;
	},
	setConfig : function(A) {
		if (A.name) {
			this.name = A.name;
			this.func = A.func;
			this[A.name] = A.func;

		}
	},

	openExcel : function() {
		logger.debug("openExcel");
		var xls = new ActiveXObject("Excel.Application");
		xls.visible = true;// false;
		this.oWB = xls.workbooks.open(this.fileName);
		this.inData = [];
		for (var i = 1; i <= 5; i++)
			this.readFromExcel(i);
		Ext.Msg.alert("情况：", "读取数据完成，目前正在存储到数据库中");
		this.storeToDataBase();
		return;
	},
	storeToDataBase : function() {
		logger.debug("storeToDataBase:"+orderExcel.date1);
		dwr.engine.setAsync(false);
		var arr = orderExcel.inData;
		for (var i = 0; i < arr.length; i++) {
			orderExcel.run(arr[i].ssign, arr[i].factory, arr[i].price,orderExcel.date1,
					function(ret) {
						if (!ret)
							Ext.Msg.alert("情况：","i data 插入到数据库时遇到错误");
					});

		}
		Ext.Msg.alert("情况：","数据插入完成");
		orderExcel.inData=[];
		dwr.engine.setAsync(true);
	},
	initReadFromExcel : function() {
		logger.debug("initReadFromExcel");
		this.openExcel();
		return;
	},
	readDate : function(A) {
		
		var date1 = Date.parseDate(A, "Y-m-d");
		return date1;
	},
	readFromExcel : function(A) {
		logger.debug("readFromExcel:" + A);
		this.oWB.worksheets(A).select();
		var oSheet = this.oWB.ActiveSheet;
		for (var i = this.defaultConfig.start_row; i < 200; i++) {
			var ssign = oSheet.Cells(i, this.defaultConfig.ssign_col).value;
			var factory = oSheet.Cells(i, this.defaultConfig.factory_col).value;
			var price = oSheet.Cells(i, this.defaultConfig.price_col).value;
			if (/N/.test(ssign)) {
				this.inData.push({
							ssign : ssign,
							factory : factory,
							price : price
						});
			}
		}
		logger.debug("exit readFromExcel:" + A);
		return;
	},
	initUI : function() {
		this.initFileUpdateForm();
		this.initFileUpdateWin();
		logger.debug("win ok");
		this.fileWin.show();
	},
	initFileUpdateForm : function() {
		if (!this.fileForm)
			this.fileForm = new Ext.FormPanel({
						fileUpload : true,
						width : 500,
						frame : true,
						autoHeight : true,
						bodyStyle : 'padding: 10px 10px 0 10px;',
						labelWidth : 80,
						defaults : {
							anchor : '95%',
							allowBlank : false,
							msgTarget : 'side'
						},
						items : [{
									xtype : 'datefield',
									format : 'Y-m-d',
									id : 'date1',
									fieldLabel : '日期',
									name : 'date1'
								}, {
									xtype : 'fileuploadfield',// 'fileuploadfield',
									id : 'form-file',
									emptyText : '请选择表单',
									fieldLabel : '配方价格位置',
									name : 'photo-path',
									regex : /.xls$/,// 判断是否输入的是excel.
									buttonCfg : {
										text : '...',
										iconCls : 'upload-icon'
									}
								}],
						buttons : [{
							text : '读取',
							handler : function() {
								if (orderExcel.fileForm.getForm().isValid()) {
									logger.debug("start to getName");
									orderExcel.date1=orderExcel.readDate(orderExcel.fileForm
											.findById("date1")
											.getRawValue());
									logger.debug("date:"+orderExcel.date1);
									var obj = orderExcel
											.setFileName((orderExcel.fileForm
													.findById("form-file")
													.getRawValue()));
									
									orderExcel.oWB.close();
									logger.debug("close success");
									
									orderExcel.fileWin.hide();
								}
							}
						}, {
							text : '重置',
							handler : function() {
								orderExcel.fileForm.getForm().reset();
							}
						}]
					});
	},
	initFileUpdateWin : function() {
		if (!this.fileWin)
			this.fileWin = new Ext.Window({
						layout : 'column',
						width : 500,
						autoHeight : true, // 460
						closeAction : 'hide',
						plain : true,
						items : this.fileForm,
						modal : false,
						title : '导入excel价格表'
					});
	},
	setProcessStore : function(store) {
		this.tempStore = store;
		this.readTag();
	},
	setOkList : function(A) {
		this.okList = A;
	},
	getName : function() {
		return this.outName;
	},
	getData : function() {
		return this.outData;
	},
	removeAll : function() {
		this.outData = [];
		this.outName = [];
		this.okList = [];
		// this.tempStore=null;
	},
	readTag : function() {
		var t = [];
		// 首先是转换最基本的那些信息
		var i = 0;
		t[i++] = "编号";
		t[i++] = "目标或实际";
		t[i++] = "月份";
		t[i++] = "年份";
		t[i++] = "规格";
		t[i++] = "分厂号";
		t[i++] = "订单号";
		t[i++] = "发货数量";
		t[i++] = "牌号";
		t[i++] = "入库牌号";
		t[i++] = "配方单价(元/公斤)";
		t[i++] = "产品入库规格";
		t[i++] = "毛重";
		t[i++] = "前道收得率";
		t[i++] = "未磨成品率";
		t[i++] = "已磨成品率";
		t[i++] = "废品率";
		t[i++] = "前道费用(元/公斤)";
		t[i++] = "产品入库成本";
		t[i++] = "规格类型";
		t[i++] = "加工流程编号";

		if (this.tempStore)
			for (var j = 0; j < this.tempStore.getCount(); j++) {

				t[i++] = this.tempStore.getAt(j).data["processName"] + "加工后规格";
				t[i++] = this.tempStore.getAt(j).data["processName"]
						+ "加工数量(片)";
				t[i++] = this.tempStore.getAt(j).data["processName"]
						+ "加工面积(mm)";
				t[i++] = this.tempStore.getAt(j).data["processName"] + "加工成品率";
				t[i++] = this.tempStore.getAt(j).data["processName"] + "加工提升";
				t[i++] = this.tempStore.getAt(j).data["processName"] + "加工单价";

				if (this.tempStore.getAt(j).data["processName"] == '芯子') {
					t[i++] = this.tempStore.getAt(j).data["processName"]
							+ "加工成本(元/只)";
					t[i++] = "扣除" + this.tempStore.getAt(j).data["processName"]
							+ "后成本(元/只)";
				} else {
					t[i++] = this.tempStore.getAt(j).data["processName"]
							+ "加工费(元/只)";
					t[i++] = this.tempStore.getAt(j).data["processName"]
							+ "后成本(元/只)";
				}
			}
		t[i++] = "镀层";
		t[i++] = "包装费";
		t[i++] = "后道收得率";
		t[i++] = "订单成本";
		t[i++] = "订单日期";
		t[i++] = "销售价格";
		t[i++] = "毛利润";
		t[i++] = "备注";
		t[i++] = "结算人";
		t[i++] = "结算日期";

		this.outName = t;
		this.tempStore = null;
	},
	readData : function(arr, subList) {
		var obj = [];
		var k = 0;
		obj[k++] = arr.data["id"];
		obj[k++] = arr.data["status"];
		obj[k++] = arr.data["month1"];
		obj[k++] = arr.data["year1"];
		obj[k++] = arr.data["spec"];
		obj[k++] = arr.data["factory"];
		obj[k++] = arr.data["transnum"];
		obj[k++] = arr.data["ordernum"];
		obj[k++] = arr.data["ssign"];
		obj[k++] = arr.data["ssign_yz"];
		obj[k++] = arr.data["peifang"];
		obj[k++] = arr.data["spec_yz"];
		obj[k++] = arr.data["grossweight"];
		obj[k++] = arr.data["qiandao_yield"];

		obj[k++] = arr.data["weimo"];
		obj[k++] = arr.data["yimo"];
		obj[k++] = arr.data["feipin"];
		obj[k++] = arr.data["qiandao_cost"];
		obj[k++] = arr.data["product_cost_before"];
		obj[k++] = arr.data["specType"];
		obj[k++] = arr.data["processScheme"];

		for (var j = 0; j < subList.length; j++) {
			obj[k++] = subList[j].afterSpec;
			obj[k++] = subList[j].processNum;
			obj[k++] = subList[j].processArea;
			obj[k++] = subList[j].processYield;
			obj[k++] = subList[j].processUpdate;
			obj[k++] = subList[j].unitPrice;
			obj[k++] = subList[j].processPrice;
			obj[k++] = subList[j].untilPrice;
		}
		obj[k++] = arr.data["plate"];
		obj[k++] = arr.data["baozhuangfei"];
		obj[k++] = arr.data["houdao_yield"];
		obj[k++] = arr.data["product_cost_last"];
	
		if(arr.data["date1"].format!== undefined )
		obj[k++] = arr.data["date1"].format('Y-m-d');
		else
		obj[k++] = arr.data["date1"];
		obj[k++] = arr.data["ordercost"];
		obj[k++] = arr.data["lirun"];
		obj[k++] = arr.data["bz"];
		obj[k++] = arr.data["maker"];
		if(arr.data["makeDate"].format!== undefined )
		obj[k++] = arr.data["makeDate"].format('Y-m-d');
		else
		obj[k++] = arr.data["makeDate"];
		this.outData.push(obj);
	},
	print : function() {
		var oXL = new ActiveXObject("Excel.Application");
		var oWB = oXL.Workbooks.Add();
		var oSheet = oWB.ActiveSheet;
		oXL.visible = true;

		oSheet.Cells(2, 1).value = "通城公司的订单结算信息";
		oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).mergecells = true;
		oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).Font.Name = "Arial";
		oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).Font.Size = 14;
		oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).VerticalAlignment = 3;
		oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).HorizontalAlignment = 3;
		var record = [];
		// 3行显示报价单
		var m = 0;
		for (var i = 0; i < this.outName.length && m < this.okList.length; i++) {// 第一,二列显示tab，第三列开始显示数据

			if (this.okList[m] != i)
				continue;
			m++;
			oSheet.Cells(3 + m, 1).value = this.outName[i];
			oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).Font.Size = 13;
			oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).Font.Bold = true;
			oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 2)).mergecells = true;
			if (/规格/.test(this.outName[i]))
				record.push(i);
			oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).VerticalAlignment = 3;
			oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).HorizontalAlignment = 3;

		}
		record.reverse();
		var start = 3;

		var k = 0;

		for (var i = 0; i < this.outData.length; i++) {
			var obj = this.outData[i];
			m = 0;

			for (var j = 0; j < obj.length && m < this.okList.length; j++) {
				// alert(obj[j]);
				if (this.okList[m] != j)
					continue;
				m++;
				// alert(obj[j]);
				oSheet.Cells(3 + m, start + i).value = obj[j];
				if (j == record[k]) {
					// oSheet.Range(oSheet.Cells(3+j, start+i),
					// oSheet.Cells(3+j,
					// start+i)).Font.Size=10;
					oSheet.Range(oSheet.Cells(3 + m, start + i), oSheet.Cells(3
											+ m, start + i)).Font.Bold = true;
					k++;
				}
			}
			oSheet.Range(oSheet.Cells(3 + 0, start + i), oSheet.Cells(3
									+ obj.length, start + i)).VerticalAlignment = 3;
			oSheet.Range(oSheet.Cells(3 + 0, start + i), oSheet.Cells(3
									+ obj.length, start + i)).HorizontalAlignment = 3;
		}
	}

}

function writeToExcel(tab, storeList, okList) {
	// alert("start to exce.js");
	var oXL = new ActiveXObject("Excel.Application");
	var oWB = oXL.Workbooks.Add();
	var oSheet = oWB.ActiveSheet;
	oXL.visible = true;

	oSheet.Cells(2, 1).value = "通城公司的订单结算信息";
	oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).mergecells = true;
	oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).Font.Name = "Arial";
	oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).Font.Size = 14;
	oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).VerticalAlignment = 3;
	oSheet.Range(oSheet.Cells(2, 1), oSheet.Cells(2, 10)).HorizontalAlignment = 3;
	var record = [];
	// 3行显示报价单
	var m = 0;
	for (var i = 0; i < tab.length && m < okList.length; i++) {// 第一,二列显示tab，第三列开始显示数据

		if (okList[m] != i)
			continue;
		m++;
		oSheet.Cells(3 + m, 1).value = tab[i];
		oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).Font.Size = 13;
		oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).Font.Bold = true;
		oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 2)).mergecells = true;
		if (/规格/.test(tab[i]))
			record.push(i);
		oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).VerticalAlignment = 3;
		oSheet.Range(oSheet.Cells(3 + m, 1), oSheet.Cells(3 + m, 1)).HorizontalAlignment = 3;

	}
	record.reverse();
	var start = 3;

	var k = 0;

	for (var i = 0; i < outData.length; i++) {
		var obj = outData[i];
		m = 0;
		// alert(obj.length+":m:"+m+"::"+okList.length);
		for (var j = 0; j < obj.length && m < okList.length; j++) {
			// alert(obj[j]);
			if (okList[m] != j)
				continue;
			m++;
			// alert(obj[j]);
			oSheet.Cells(3 + m, start + i).value = obj[j];
			if (j == record[k]) {
				// oSheet.Range(oSheet.Cells(3+j, start+i), oSheet.Cells(3+j,
				// start+i)).Font.Size=10;
				oSheet.Range(oSheet.Cells(3 + m, start + i), oSheet.Cells(
								3 + m, start + i)).Font.Bold = true;
				k++;
			}
		}
		oSheet.Range(oSheet.Cells(3 + 0, start + i), oSheet.Cells(3
								+ obj.length, start + i)).VerticalAlignment = 3;
		oSheet.Range(oSheet.Cells(3 + 0, start + i), oSheet.Cells(3
								+ obj.length, start + i)).HorizontalAlignment = 3;
	}
	// interval = window.setInterval("Cleanup();", 1);
}

var orderExcel = new OrderExcel();
